using System;
using JNPF.Basics.Models;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using JNPF.Utils;
using System.Transactions;
using MimeKit.Cryptography;

namespace JNPF.Basics.Logic
{
    public class CustomerloanBll
    {
        private CustomerloanIService service = new CustomerloanService();
        private CustomerloandetailIService detailservice = new CustomerloandetailService();

        private OrganizeIService organizeService = new OrganizeService();


        /// <summary>
        /// 获取信息
        /// </summary>
        /// <param name="id">主键值</param>
        /// <returns></returns>
        public async Task<CustomerloanEntity> GetInfo(int id)
        {
            try
            {
                return await service.GetInfo(id);
            }
            catch (Exception ex)
            {
                throw ExceptionEx.Throw(ex);
            }
        }

        /// <summary>
        /// 获取信息
        /// </summary>
        /// <param name="id">主键值</param>
        /// <returns></returns>
        public async Task<CustomerloanEntity> GetInfoCustomerNeedRecordId(int id)
        {
            try
            {
                return await service.GetInfoCustomerNeedRecordId(id);
            }
            catch (Exception ex)
            {
                throw ExceptionEx.Throw(ex);
            }
        }


        /// <summary>
        /// 获取信息
        /// </summary>
        /// <param name="id">主键值</param>
        /// <returns></returns>
        public async Task<CustomerloanEntity> GetInfoByUUId(string uuid)
        {
            try
            {
                return await service.GetInfoByUUId(uuid);
            }
            catch (Exception ex)
            {
                throw ExceptionEx.Throw(ex);
            }
        }



        /// <summary>
        /// 获取信息
        /// </summary>
        /// <param name="uuids">主键id</param>
        /// <returns></returns>
        public async Task<List<CustomerloanEntity>> GetInfoByUUId(List<string> uuids)
        {
            try
            {
                return await service.GetInfoByUUId(uuids);
            }
            catch (Exception ex)
            {
                throw ExceptionEx.Throw(ex);
            }
        }


        /// <summary>
        /// 获取列表
        /// </summary>
        /// <param name="input">请求参数</param>
        /// <returns></returns>
        public async Task<PageResult<CustomerloanEntity>> GetPageList(CustomerloanListQueryInput input)
        {
            try
            {
                return await service.GetPageList(input);
            }
            catch (Exception ex)
            {
                throw ExceptionEx.Throw(ex);
            }
        }

        /// <summary>
        /// 创建信息
        /// </summary>
        /// <param name="entity">实体对象</param>
        /// <returns></returns>
        public async Task<int> Create(CustomerloanEntity entity)
        {
            try
            {
                entity.CreatorTime = DateTime.Now;
                entity.CreatorUserId = UserProvider.Instance.Get().UserId;
                entity.CreatorUser = UserProvider.Instance.Get().UserName;
                entity.UUID = Guid.NewGuid().ToString("N");
                entity.DeleteMark = 0;
                CustomerloandetailEntity loadetailEn = new CustomerloandetailEntity();

                ///1、查询信贷管家
                ///2、四个提成比例
                ///3、查询客户经理名字和部门
                ///4、默认首贷
                ///4、查询合伙人信息
                ///生成子表
                ///

                //信贷管家
                CustomeruserIService cus = new CustomeruserService();
                var xindai = await cus.GetInfo(entity.CustomerId.Value, 1);
                if (xindai != null)
                {
                    entity.UserId = xindai.UserId;


                    var xindaiEn = await new UserService().GetInfo(xindai.UserId);
                    if (xindaiEn != null)
                    {
                        entity.UserName = xindaiEn.RealName;





                    }


                }



                //客户经理

                var BankUser = await new UserService().GetInfo(entity.BankUserId);
                if (BankUser != null)
                {

                    entity.BankUserId = BankUser.Id;
                    entity.BankUserName = BankUser.NickName;

                    OrganizeEntity organizeEntity = await organizeService.GetInfo(BankUser.OrganizeId);
                    if (organizeEntity != null)
                    {
                        entity.BankName = organizeEntity.FullName;

                    }
                }
                //合伙人，先取企业创建人

                var cunstomerEN = await new CustomerbaseService().GetInfo(entity.CustomerId.Value);
                if (cunstomerEN != null)
                {
                    var userInfo = await new UserService().GetInfo(cunstomerEN.PartnerUserId);
                    if (userInfo != null)//找到合伙人
                    {

                        //取扩展表

                        UserextendIService uis = new UserextendService();
                        UserextendListQueryInput qil = new UserextendListQueryInput();
                        qil.userId = userInfo.Id;
                        var extend = uis.GetList(qil).Result.FirstOrDefault();
                        if (extend != null)
                        {
                            //四个比例赋值
                            //entity.First = extend.First;
                            //entity.Second = extend.Second;
                            //entity.OnceMore = extend.OnceMore;
                            entity.DrawProportion = extend.DrawProportion;
                            loadetailEn.PartnerUserId = userInfo.Id;
                            loadetailEn.PartnerUserRealName = userInfo.RealName;
                            loadetailEn.ParentPartnerUserId = extend.ParentPartnerUserId;
                            var parentPartnerUser = await new UserService().GetInfo(extend.ParentPartnerUserId);
                            if (parentPartnerUser != null)
                            {

                                loadetailEn.ParentPartnerUserRealName = parentPartnerUser.RealName;

                            }
                        }

                    }


                }

                var ret = 0;
                if (await service.Create(entity) > 0)
                {
                    loadetailEn.CreatorTime = DateTime.Now;
                    loadetailEn.CreatorUserId = UserProvider.Instance.Get().UserId;
                    loadetailEn.CreatorUser = UserProvider.Instance.Get().UserName;
                    loadetailEn.CustomerLoanUUID = entity.UUID;
                    loadetailEn.DeleteMark = 0;
                    loadetailEn.Serialid = Guid.NewGuid().ToString("N");

                    //合伙人，先取企业创建人

                    if (cunstomerEN != null)
                    {


                        //if (entity.Number == 1)//合伙人提成
                        //{
                        //    loadetailEn.PartnerPoint = entity.Amount * 10000 * (entity.First.Value / 100);
                        //    loadetailEn.PartnerPoints = entity.First.Value;


                        //}
                        //else if (entity.Number == 2)//合伙人提成
                        //{
                        //    loadetailEn.PartnerPoint = entity.Amount * 10000 * (entity.Second.Value / 100);
                        //    loadetailEn.PartnerPoints = entity.Second.Value;

                        //}
                        //else if (entity.Number >= 3)//合伙人提成
                        //{
                        //    loadetailEn.PartnerPoint = entity.Amount * 10000 * (entity.OnceMore.Value / 100);
                        //    loadetailEn.PartnerPoints = entity.OnceMore.Value;

                        //}
                        //if (!string.IsNullOrEmpty(loadetailEn.ParentPartnerUserId))//如果有上级，计算抽佣
                        //{
                            
                        //    loadetailEn.ParentUserPoints = loadetailEn.PartnerPoint * (entity.DrawProportion / 100);
                        //    loadetailEn.ParentPartnerPoints = entity.DrawProportion;

                        //}


                    }
                    loadetailEn.Type = 0;//跟进中

                    ret = await detailservice.Create(loadetailEn);
                }
                return ret;
            }
            catch (Exception ex)
            {
                throw ExceptionEx.Throw(ex);
            }
        }

        /// <summary>
        /// 更新信息
        /// </summary>
        /// <param name="id">主键值</param>
        /// <param name="entity">实体对象</param>
        /// <returns></returns>
        public async Task<int> Update(int id, CustomerloanEntity entity)
        {
            try
            {
                entity.Id = id;
                entity.LastModifyTime = DateTime.Now;
                entity.LastModifyUserId = UserProvider.Instance.Get().UserId;
                return await service.Update(entity);
            }
            catch (Exception ex)
            {
                throw ExceptionEx.Throw(ex);
            }
        }

        /// <summary>
        /// 删除信息
        /// </summary>
        /// <param name="entity">实体对象</param>
        /// <returns></returns>
        public async Task<int> Delete(CustomerloanEntity entity)
        {
            try
            {
                entity.DeleteMark = 1;
                entity.DeleteTime = DateTime.Now; ;
                entity.DeleteUserId = UserProvider.Instance.Get().UserId;

                return await service.Update(entity);
            }
            catch (Exception ex)
            {
                throw ExceptionEx.Throw(ex);
            }
        }

        /// <summary>
        /// 获取列表
        /// </summary>
        /// <param name="input">过滤条件</param>
        /// <returns></returns>
        public async Task<List<CustomerloanEntity>> GetList(CustomerloanListQueryInput input)
        {
            try
            {
                return await service.GetList(input);
            }
            catch (Exception ex)
            {
                throw ExceptionEx.Throw(ex);
            }
        }


    }
}